﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;

namespace WPReceiveToast
{
    public partial class MainPage : PhoneApplicationPage
    {
        private string channelName = "Test";
        private HttpNotificationChannel channel;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            channel = HttpNotificationChannel.Find(channelName);

            if (channel == null)
            {
                channel = new HttpNotificationChannel(channelName);
                channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated);
                channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
                channel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(channel_ShellToastNotificationReceived);
                channel.Open();
                channel.BindToShellToast();
            }
            else
            {
                channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated);
                channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
                channel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(channel_ShellToastNotificationReceived);
            }
        }

        void channel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
        {
            Dispatcher.BeginInvoke(
                () => MessageBox.Show(string.Format("toast empfangen {0} {1}", e.Collection["wp:Text1"], e.Collection["wp:Text2"]))
                );
        }

        void channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
        {

        }

        void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
        {

        }
    }
}